package com.eqxiu.ieditor.commons.crypt;

import java.security.MessageDigest;

/**
 * MD5加密工具类
 * 
 * @author zyq
 * 
 */
public class MD5Util {

    private static final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",
            "f" };

    public static String encode(String password) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] byteArray = md5.digest(password.getBytes("utf-8"));
            String passwordMD5 = byteArrayToHexString(byteArray);
            return passwordMD5;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return password;
    }

    private static String byteArrayToHexString(byte[] byteArray) {
        StringBuffer sb = new StringBuffer();
        for (byte b : byteArray) {
            sb.append(byteToHexChar(b));
        }
        return sb.toString();
    }

    private static Object byteToHexChar(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hex[d1] + hex[d2];
    }

    public static String addSalt(String plaintext, String salt) {
        return plaintext + "{" + salt + "}";
    }

    public static void main(String args[]) {
        // String str =
        // "cn.ql.module.behavior.dao.ContentForumDao.queryDataByParams.{resourceId=ff8080813bcadccc013bcdfad96e19d9,forumStatus=1,pagingNumberPer=20,pIsNull=value,studyType=ZT,view=all,planId=ff8080813bcadccc013bcda44819161d,studyPlanId=ff8080813bcadccc013bcda44819161d,pagingPage=1}";
        // Long b1 = System.currentTimeMillis();
        // 1万次128毫秒
        // for(int i =0;i<10000;i++){
        // MD5Util.encode(str);
        // Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        // //md5.setEncodeHashAsBase64(false);
        // //md5.encodePassword(str, "");
        // }
        // Long e1 = System.currentTimeMillis();
        // System.out.println(e1-b1);

        // System.out.println(MD5Util.encode("oYfuOjsz1BFXIevo4jirgJNcAtYM"));

        System.out.println(MD5Util.encode(MD5Util.addSalt("1", "99@qq.com")));

    }

}
